package com.example.service.impl;

import com.example.entity.Grade;
import com.example.mapper.GradeMapper;
import com.example.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class GradeServiceImpl implements GradeService {

    @Autowired
    private GradeMapper gradeMapper;

    @Override
    public boolean addGrade(Grade grade) {
        // 设置创建时间和更新时间
        grade.setCreateTime(new Date());
        grade.setUpdateTime(new Date());
        // 默认为未删除状态
        grade.setDeleted(0);
        return gradeMapper.insert(grade) > 0;
    }

    @Override
    public boolean updateGrade(Grade grade) {
        // 更新更新时间
        grade.setUpdateTime(new Date());
        return gradeMapper.updateById(grade) > 0;
    }

    @Override
    public boolean deleteGrade(Long id) {
        // 逻辑删除，设置删除标志
        Grade grade = new Grade();
        grade.setId(id);
        grade.setDeleted(1);
        grade.setUpdateTime(new Date());
        return gradeMapper.updateById(grade) > 0;
    }

    @Override
    public Grade getGradeById(Long id) {
        return gradeMapper.selectById(id);
    }

    @Override
    public List<Grade> getGradeList(Grade grade) {
        return gradeMapper.selectList(grade);
    }

    @Override
    public List<Grade> getGradesByStudentId(Long studentId) {
        return gradeMapper.selectByStudentId(studentId);
    }

    @Override
    public List<Grade> getGradesByCourseId(Long courseId) {
        return gradeMapper.selectByCourseId(courseId);
    }

    @Override
    public Map<String, Object> getCourseStatistics(Long courseId) {
        // 获取该课程的所有成绩
        List<Grade> grades = gradeMapper.selectByCourseId(courseId);

        if (grades == null || grades.isEmpty()) {
            return Collections.singletonMap("message", "暂无成绩数据");
        }

        // 计算平均分、最高分、最低分
        double averageScore = grades.stream()
                .mapToDouble(Grade::getScore)
                .average()
                .orElse(0.0);

        double maxScore = grades.stream()
                .mapToDouble(Grade::getScore)
                .max()
                .orElse(0.0);

        double minScore = grades.stream()
                .mapToDouble(Grade::getScore)
                .min()
                .orElse(0.0);

        // 统计各分数段人数
        Map<String, Integer> scoreDistribution = new LinkedHashMap<>();
        scoreDistribution.put("0-59", 0);
        scoreDistribution.put("60-69", 0);
        scoreDistribution.put("70-79", 0);
        scoreDistribution.put("80-89", 0);
        scoreDistribution.put("90-100", 0);

        grades.forEach(grade -> {
            double score = grade.getScore();
            if (score < 60) {
                scoreDistribution.put("0-59", scoreDistribution.get("0-59") + 1);
            } else if (score < 70) {
                scoreDistribution.put("60-69", scoreDistribution.get("60-69") + 1);
            } else if (score < 80) {
                scoreDistribution.put("70-79", scoreDistribution.get("70-79") + 1);
            } else if (score < 90) {
                scoreDistribution.put("80-89", scoreDistribution.get("80-89") + 1);
            } else {
                scoreDistribution.put("90-100", scoreDistribution.get("90-100") + 1);
            }
        });

        Map<String, Object> result = new HashMap<>();
        result.put("averageScore", averageScore);
        result.put("maxScore", maxScore);
        result.put("minScore", minScore);
        result.put("distribution", scoreDistribution);

        return result;
    }
}